// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
let panelBase = require("panelBase")
let viewConfig = {
    hbPrefab:{
        node:null,
        name:"bg/hb",
        active:false
    },
    hbParent:{
        node:null,
        name:"bg",
    }
}
cc.Class({
    extends: panelBase,
    

    properties: {
        hbPool:cc.NodePool,
        isSelected:false,
        maxCount:50,
        curCount:0,
        hbList:[],
    },

    init(){
        this._super(viewConfig);

        cc.tg.eventCenter.addEvent("onHBRain",()=>{
            this.onShow();
        })
      //  this.hbPool = new cc.NodePool();
        this.maxCount = 50;
        let height = cc.winSize.height;
        let width  = cc.winSize.width;
        for(let i = 0 ; i < this.maxCount;++i)
        {
            let hb = cc.instantiate(viewConfig["hbPrefab"].node);
            hb.setPosition(cc.v2(width*0.03*i - (width *0.4),height*0.6))
            hb.name = (i+1).toString();
            hb.active = true;
            this.hbList.push(hb)
           // this.hbPool.put(hb);
        }
       // cc.log(this.hbList.length);
        this.curCount = 0;
        this.isInited = true;
    },

    onShow(){
        this._super();
        for(let i = 0 ; i < this.maxCount;++i)
        {
            let hb = this.hbList[i];
            
            this.toFall(hb,i%10);
        }
        setTimeout(()=>{
            
            cc.tg.eventCenter.dispatchEvent("toShowAd",{codeId:"hby_ex",countDown:3});
            cc.tg.eventCenter.dispatchEvent("showPanel",{panelName:"banner",active:true});
            this.onHide();
        },2000)
    },


    toFall(node,index){
        if(!node)
            return ;
        let delay = cc.delayTime(index*0.15)
        let speed = Math.random()*100 + 150;
        let height = cc.winSize.height*1.1;
        let time = (height / speed).toFixed(1);
        let cb = ()=>{
           // this.hbPool.put(node);
            this.resetPosY(node);
        }
        let moveDown = cc.moveBy(time,cc.v2(0,-height)).easing(cc.easeBackOut())
       // cc.log(time);
        node.parent = viewConfig["hbParent"].node;
        node.active = true;
        node.runAction(cc.sequence(delay,moveDown,cc.callFunc(cb)));
    },

    onHBClick(event){
        if(this.isSelected)
            return ;
        this.isSelected = true;
        event.target.stopAllActions();
    },

    onHide(){
        
        //let p = viewConfig["hbParent"].node
        for(let i = this.hbList.length-1 ;i >= 0;--i)
        {
           // this.hbPool.put(this.hbList[i]);
            this.resetPosY(this.hbList[i]);
        }
        this.isSelected = false;
        this._super();
    },

    resetPosY(node){
        if(!node)
            return ;
        let height = cc.winSize.height;
        node.y = height * 0.6;
        node.parent = null;
        node.stopAllActions();
    },
    onDestroy(){
        for(let key in viewConfig)
        {
            viewConfig[key].node = null;
        }
    }
});
